var fs = require('fs')
var crypt = require(PATH+'/libs/crypt')
var Front = require(PATH+'/controllers/front')
var async = require('async')

module.exports = function(app){
	app.use(function(req, res, next) {
		var queue = async.queue(function(data,callback){
			callback()
		})
		queue.push(null,function(){
			fs.exists(PATH+'/data/sys.json',function(exists){
				if(exists){
					fs.readFile(PATH+'/data/sys.json',function(err,data){
						if(err)	console.log(err)
						global.sys = JSON.parse(data)
					})
				}else{
					global.sys = {}
				}
			})
		})
		queue.push(null,function(){
			fs.exists(PATH+'/data/menus.json',function(exists){
				if(exists){
					fs.readFile(PATH+'/data/menus.json',function(err,data){
						if(err)	console.log(err)
						var menus = JSON.parse(data)
						for(var i in menus){
							menus[i].href = crypt.en(menus[i].href)
						}
						global.menus = menus
					})
				}else{
					global.menus = {}
				}
			})
		})
		queue.drain = next
	})

	// 首页
	app.get('/',Front.index)
	app.get('/index',Front.index)

	// 电影页
	app.get('/movie/:url',Front.movie)

	// 分类页
	app.get('/cate/:url',Front.cate)

	// 分类页
	app.get('/search',Front.search)

	// api
	app.get('/weixin_new',Front.weixin_new)
	app.get('/weixin_search/:keywords',Front.weixin_search)

	app.get('/robots.txt',function(req, res){
		res.redirect('/static/robots.txt')
	})
}

